//
//  ViewController.m
//  17作业
//
//  Created by dllo on 16/8/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "FirstViewController.h"
#import "CustomTableViewCell.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *arr;
@property (nonatomic,strong) NSMutableArray *nameArr;
@property (nonatomic,strong) NSMutableArray *modelArr;
//@property (nonatomic,strong) NSMutableArray *genderArr;
//@property (nonatomic,strong) NSMutableArray *phoneNumberArr;
//@property (nonatomic,strong) NSMutableArray *ageArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    
   NSArray *arr = [NSArray arrayWithContentsOfFile:@"/Users/dllo/DLS160710/UI/HomeWork/17作业/17作业/DLS160710(1).plist"];
    _nameArr = [NSMutableArray array];
    _modelArr = [NSMutableArray array];
//    _genderArr = [NSMutableArray array];
//    _phoneNumberArr = [NSMutableArray array];
//    _ageArr = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        NSString *nameStr = [dic objectForKey:@"name"];
        NSString *genderStr = [dic objectForKey:@"gender"];
        NSString *phoneNumberStr = [dic objectForKey:@"phoneNumber"];
        NSNumber *ageStr1 = [dic objectForKey:@"age"];
        NSString *ageStr = [ageStr1 stringValue];
        Model *model = [[Model alloc] initWithName:nameStr gender:genderStr phoneNumber:phoneNumberStr age:ageStr];
//         NSLog(@"%@",model.name);
        [_nameArr addObject:nameStr];
        [_modelArr addObject:model];
//         NSLog(@"%@",model.phoneNumber);
//        [_genderArr addObject:genderStr];
//        [_phoneNumberArr addObject:phoneNumberStr];
//        [_ageArr addObject:ageStr];
    }
    
    NSLog(@"%@",_modelArr);
   
    
    

    
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"cell"];
    _tableView.rowHeight = 80;
    
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addContact:)];
    self.navigationItem.rightBarButtonItem = addBtn;
    self.navigationItem.leftBarButtonItem = self.editButtonItem;
}

- (void)addContact:(UIBarButtonItem *)btn
{
    Model *model = [_modelArr firstObject];
    [_modelArr insertObject:model atIndex:0];
    [_tableView reloadData];
}


//1
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [_tableView setEditing:editing animated:animated];
    
}
//2
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
//3
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _nameArr.count;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        NSLog(@"插入");
        [_nameArr insertObject:@"小子" atIndex:indexPath.row];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    } else if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"删除");
        [_nameArr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    cell.textLabel.text = _nameArr[indexPath.row];
    cell.model = _modelArr[indexPath.row];
    return cell;

}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    [_nameArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    
}

//- (void)reloadData
//{
//    [_tableView reloadData];
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FirstViewController *fvc = [[FirstViewController alloc] init];
    fvc.model = _modelArr[indexPath.row];
    [self.navigationController pushViewController:fvc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
